/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.model;

import org.eclipse.swt.graphics.Image;
import java.util.ArrayList;

@SuppressWarnings("unused")
public class UMLStateMachine extends UMLEntity {
	private static final long serialVersionUID = -1761567388615011472L;
	private String name;
	private ArrayList<UMLCompositeState> compositeStates;
	private ArrayList<UMLState> states;
	private ArrayList<UMLStartState> startStates;
	private ArrayList<UMLEndState> endStates;
	private ArrayList<UMLRegion> regions;
	private static final Image ICON = createImage("icons/state/state_machine_small.gif");
	private ModelElement parent;

	public UMLStateMachine() {
		super();
		this.states = new ArrayList<UMLState>();
		this.compositeStates = new ArrayList<UMLCompositeState>();
		this.startStates = new ArrayList<UMLStartState>();
		this.endStates = new ArrayList<UMLEndState>();
		this.regions = new ArrayList<UMLRegion>();
	}

	public UMLStateMachine(String name) {
		super();
		this.name = name;
		this.states = new ArrayList<UMLState>();
		this.compositeStates = new ArrayList<UMLCompositeState>();
		this.startStates = new ArrayList<UMLStartState>();
		this.endStates = new ArrayList<UMLEndState>();
		this.regions = new ArrayList<UMLRegion>();
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public Image getIcon() {
		return ICON;
	}

	public String getLabelText() {
		String labelText = " <<stateMachine>> ";
		labelText += name + " ";
		return labelText;
	}

	public String toString() {
		return name;
	}

	public ArrayList<UMLState> getStates() {
		return states;
	}

	public ArrayList<UMLCompositeState> getCompositeStates() {
		return compositeStates;
	}

	public ArrayList<UMLStartState> getStartStates() {
		return startStates;
	}

	public ArrayList<UMLEndState> getEndStates() {
		return endStates;
	}

	public ArrayList<UMLRegion> getRegions() {
		return regions;
	}

	public boolean addState(UMLState newState) {
		if (!states.contains(newState)) {
			states.add(newState);
			firePropertyChange(CHILD, null, newState);
			return true;
		}
		return false;
	}

	public boolean removeState(UMLState newState) {
		if (states.remove(newState)) {
			firePropertyChange(CHILD, newState, null);
			return true;
		}
		return false;
	}

	public boolean equals(Object o) {
		if (o == null || !(o instanceof UMLStateMachine))
			return false;
		UMLStateMachine c = (UMLStateMachine) o;
		String text1 = getLabelText();
		String text2 = c.getLabelText();
		return text1.equals(text2);
	}

	public boolean addCompositeState(UMLCompositeState newState) {
		if (!compositeStates.contains(newState)) {
			compositeStates.add(newState);
			firePropertyChange(CHILD, null, newState);
			return true;
		}
		return false;
	}

	public boolean removeCompositeState(UMLCompositeState newState) {
		if (compositeStates.remove(newState)) {
			firePropertyChange(CHILD, newState, null);
			return true;
		}
		return false;
	}

	public boolean addStartState(UMLStartState newState) {
		if (!startStates.contains(newState)) {
			startStates.add(newState);
			firePropertyChange(CHILD, null, newState);
			return true;
		}
		return false;
	}

	public boolean removeStartState(UMLStartState newState) {
		if (startStates.remove(newState)) {
			firePropertyChange(CHILD, newState, null);
			return true;
		}
		return false;
	}

	public boolean addEndState(UMLEndState newState) {
		if (!endStates.contains(newState)) {
			endStates.add(newState);
			firePropertyChange(CHILD, null, newState);
			return true;
		}
		return false;
	}

	public boolean removeEndState(UMLEndState newState) {
		if (endStates.remove(newState)) {
			firePropertyChange(CHILD, newState, null);
			return true;
		}
		return false;
	}

	public boolean addRegion(UMLRegion newState) {
		if (!regions.contains(newState)) {
			regions.add(newState);
			firePropertyChange(CHILD, null, newState);
			return true;
		}
		return false;
	}

	public boolean removeRegion(UMLRegion newState) {
		if (regions.remove(newState)) {
			firePropertyChange(CHILD, newState, null);
			return true;
		}
		return false;
	}

	public void setParent(ModelElement parent) {
		this.parent = parent;
	}
}
